package jfparted;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;

import javaforce.*;

/**
 *
 * Created : Feb 19, 2012
 *
 * @author pquiring
 */
public class ModifyPartition extends javax.swing.JDialog {

  /**
   * Creates new form AddPart
   */
  public ModifyPartition(java.awt.Frame parent, boolean modal, Data.Partition part, boolean edit, boolean mountNow) {
    super(parent, modal);
    initComponents();
    setPosition();
    if (!mountNow) {
      genfstab.setVisible(false);
    }
    start.setText(part.start);
    end.setText(part.end);
    if (part.mount.equals("/")) mount.setSelectedIndex(1);
    if (part.mount.equals("swap")) mount.setSelectedIndex(2);
    for(int a=0;a<filesys.getItemCount();a++) {
      if (filesys.getItemAt(a).equals(part.filesys)) {
        filesys.setSelectedIndex(a);
        break;
      }
    }
    initStart = part.start;
    initEnd = part.end;
    if (edit) {
      if (part.type.equals("primary")) type.setSelectedIndex(0);
      if (part.type.equals("extended")) type.setSelectedIndex(1);
      if (part.type.equals("logical")) type.setSelectedIndex(2);
      type.setEditable(false);
      filesys.setEditable(false);
      start.setEditable(false);
      end.setEditable(false);
      setTitle("Change Partition");
    } else {
      if (Data.isLogical(part)) {
        type.removeItemAt(1);
        type.removeItemAt(0);
      } else {
        if (Data.deviceHasExtPart(part.device)) {
          type.removeItemAt(1);
        } else {
          type.removeItemAt(2);
        }
      }
      setTitle("Add New Partition");
    }
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    type = new javax.swing.JComboBox();
    jLabel2 = new javax.swing.JLabel();
    mount = new javax.swing.JComboBox();
    jLabel3 = new javax.swing.JLabel();
    filesys = new javax.swing.JComboBox();
    jLabel4 = new javax.swing.JLabel();
    start = new javax.swing.JTextField();
    jLabel5 = new javax.swing.JLabel();
    end = new javax.swing.JTextField();
    ok = new javax.swing.JButton();
    cancel = new javax.swing.JButton();
    label1 = new javax.swing.JLabel();
    reset = new javax.swing.JButton();
    format = new javax.swing.JCheckBox();
    jLabel7 = new javax.swing.JLabel();
    label = new javax.swing.JTextField();
    genfstab = new javax.swing.JCheckBox();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    jLabel1.setText("Type");

    type.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "primary", "extended", "logical" }));

    jLabel2.setText("Mount Point");

    mount.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "none", "/", "swap", "/mnt/restore" }));

    jLabel3.setText("File System");

    filesys.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "none", "ext4", "ext3", "ext2", "ntfs", "swap", "fat32", "fat16" }));

    jLabel4.setText("Start");

    start.setText("0GB");

    jLabel5.setText("End");

    end.setText("0GB");

    ok.setText("OK");
    ok.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        okActionPerformed(evt);
      }
    });

    cancel.setText("Cancel");
    cancel.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        cancelActionPerformed(evt);
      }
    });

    label1.setText("NOTE : Changes are written to device instantly.");

    reset.setText("Reset");
    reset.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        resetActionPerformed(evt);
      }
    });

    format.setText("Format Partition");

    jLabel7.setText("Label");

    genfstab.setText("Update Startup Mount Table (fstab)");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addGap(0, 0, Short.MAX_VALUE)
            .addComponent(ok)
            .addGap(18, 18, 18)
            .addComponent(cancel)
            .addGap(18, 18, 18)
            .addComponent(reset))
          .addComponent(label1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(jLabel2)
              .addComponent(jLabel3)
              .addComponent(jLabel1)
              .addComponent(jLabel4)
              .addComponent(jLabel5)
              .addComponent(jLabel7))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(type, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
              .addComponent(mount, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
              .addComponent(filesys, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
              .addComponent(start)
              .addComponent(end)
              .addComponent(label)))
          .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(genfstab)
              .addComponent(format))
            .addGap(0, 0, Short.MAX_VALUE)))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel1)
          .addComponent(type, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel2)
          .addComponent(mount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel3)
          .addComponent(filesys, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel4)
          .addComponent(start, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel5)
          .addComponent(end, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel7)
          .addComponent(label, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(format)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(genfstab)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(label1)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(ok)
          .addComponent(cancel)
          .addComponent(reset))
        .addContainerGap())
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
    dispose();
  }//GEN-LAST:event_cancelActionPerformed

  private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
    if (!valid()) return;
    accepted = true;
    dispose();
  }//GEN-LAST:event_okActionPerformed

  private void resetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetActionPerformed
    start.setText(initStart);
    end.setText(initEnd);
  }//GEN-LAST:event_resetActionPerformed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton cancel;
  private javax.swing.JTextField end;
  private javax.swing.JComboBox filesys;
  private javax.swing.JCheckBox format;
  private javax.swing.JCheckBox genfstab;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JLabel jLabel5;
  private javax.swing.JLabel jLabel7;
  private javax.swing.JTextField label;
  private javax.swing.JLabel label1;
  private javax.swing.JComboBox mount;
  private javax.swing.JButton ok;
  private javax.swing.JButton reset;
  private javax.swing.JTextField start;
  private javax.swing.JComboBox type;
  // End of variables declaration//GEN-END:variables

  public boolean accepted = false;
  public String initStart, initEnd;

  public String gettype() {
    return type.getSelectedItem().toString();
  }

  public String getfilesys() {
    return filesys.getSelectedItem().toString();
  }

  public String getmount() {
    return (String)mount.getSelectedItem();
  }

  public String getstart() {
    return start.getText();
  }

  public String getend() {
    return end.getText();
  }

  public boolean getformat() {
    return format.isSelected();
  }

  public boolean getgenfstab() {
    return genfstab.isSelected();
  }

  public String getlabel() {
    return label.getText();
  }

  private void cleanLabel() {
    String str = label.getText();
    char ca[] = str.toCharArray();
    StringBuilder sb = new StringBuilder();
    for(int a=0;a<ca.length;a++) {
      if ( (ca[a] >= '0') && (ca[a] <= '9')) {sb.append(ca[a]); continue;}
      if ( (ca[a] >= 'a') && (ca[a] <= 'z')) {sb.append(ca[a]); continue;}
      if ( (ca[a] >= 'A') && (ca[a] <= 'Z')) {sb.append(ca[a]); continue;}
    }
    label.setText(sb.toString());
  }

  public boolean valid() {
    if (Data.getSize(start.getText()) < Data.getSize(initStart)) {
      start.setBackground(Color.red);
      return false;
    }
    start.setBackground(Color.white);
    if (Data.getSize(end.getText()) > Data.getSize(initEnd)) {
      end.setBackground(Color.red);
      return false;
    }
    end.setBackground(Color.white);
    if (format.isSelected()) {
      if (filesys.getSelectedIndex() == 0) {
        format.setBackground(Color.red);
        filesys.setBackground(Color.red);
        return false;
      }
      cleanLabel();
      int labelLength = label.getText().length();
      if ((labelLength == 0) || ((labelLength > 16))) {
        format.setBackground(Color.red);
        label.setBackground(Color.red);
        return false;
      }
    }
    format.setBackground(Color.white);
    filesys.setBackground(Color.white);
    label.setBackground(Color.white);
    return true;
  }

  private void setPosition() {
    Rectangle s = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
    Dimension d = getPreferredSize();
    setLocation(s.width/2 - d.width/2, s.height/2 - (d.height/2));
  }
}
